<?php
/**
 * crcl/base
 *
 * Copyright 2013 CRCL - André Sabosch - info@crcl.de
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

namespace crcl\base\exception;

interface ICauses
{
  /**
   * Factory
   * Creates new causes object from an array
   *
   * @static
   *
   * @param array $aCauses
   *
   * @return  \crcl\base\exception\Causes  $oCauses
   */
  public static function fromArray($aCauses);

  /**
   * Returns all causes as array
   *
   * @return array
   */
  public function toArray();

  /**
   * Sets causes
   *
   * @param array $aCauses
   */
  public function set($aCauses);

  /**
   * Get cause
   *
   * @param string  $key  Name of cause
   *
   * @return  mixed
   */
  public function get($key);

  /**
   * Adds cause
   *
   * @param string  $key    Name of cause
   * @param mixed   $value  Value of cause
   */
  public function add($key, $value);

  /**
   * Magic getter
   *
   * @use getValue()
   *
   * @param string  $sName    Name of cause
   * @param array   $aParams  Params of cause
   *
   * @return  mixed value as in getter function
   */
  public function __call($sName, $aParams);
}

class Causes implements ICauses
{
  public static function fromArray($aCauses)
  {
    $oCauses = new Causes();
    $oCauses->set($aCauses);

    return $oCauses;
  }

  public function toArray()
  {
    return $this->aCauses;
  }

  public function set($aCauses)
  {
    if (\is_null($aCauses))
    {
      $aCauses = array();
    }
    elseif (!\is_array($aCauses))
    {
      $aCauses = array($aCauses);
    }
    $this->aCauses = $aCauses;
  }

  public function add($key, $value)
  {
    $this->aCauses[$key] = $value;
  }

  public function get($key)
  {
    if (isset($this->aCauses[$key]))
    {
      return $this->aCauses[$key];
    }
    else
    {
      throw new Exception('Invalid getter',
        Exception::ERROR_CAUSES_INVALIDGETTER, array('key' => $key));
    }
  }

  public function __call($sName, $aParams)
  {
    if (\strlen($sName) > 3)
    {
      if (\substr($sName, 0, 3) == 'get')
      {
        $sKey = \substr($sName, 3);
        $sKey = \strtolower($sKey);

        return $this->get($sKey);
      }
    }
    throw new Exception('Invalid method',
      Exception::ERROR_CAUSES_INVALIDMETHOD,
      array('method' => $sName, 'params' => $aParams));
  }

  /** PRIVATE *************************************************************************************/

  private $aCauses = array();
}